import { handlerGobackClick, hint } from "../../../utils/util";
import User from "../../../models/user";

const user = new User();
const app = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    iptValue: "",
    isFocus: true,
    phone: "",
    code: "",
    registerStatus: undefined,
    forgetPassword: false,
    codeText: "获取验证码",
    isDisabled: false,
  },
  onLoad(options) {
    this.setData({
      phone: options.phone,
    });
    if (options.forgetPassword) {
      this.setData({
        forgetPassword: options.forgetPassword,
      });
    }
  },
  onReady() {
    if (this.data.phone) {
      this.getCode();
    }
  },
  onFocus: function (e) {
    var that = this;
    that.setData({ isFocus: true });
  },
  setValue: function (e) {
    var that = this;
    that.setData({ iptValue: e.detail.value });
  },
  handlerGobackClick,
  nextGetCode() {
    if (!this.data.iptValue) {
      hint("请输入验证码");
      return;
    }
    //重置密码获取验证码
    if (this.data.forgetPassword) {
      user
        .appCheckCode(this.data.phone, parseInt(this.data.iptValue))
        .then((res) => {
          wx.redirectTo({
            url: `/owner-app/pages/modify-password/index?forgetPassword=${true}`,
          });
        })
        .catch((err) => {
          console.log(err);
        });
    }
    //微信绑定获取验证码
    else {
      let { avatarUrl, nickName } = getApp().globalData.userInfo;
      user
        .appCheckCode(this.data.phone, parseInt(this.data.iptValue))
        .then((res) => {
          if (res.code === 1000) {
            if (this.data.registerStatus === 0) {
              wx.navigateTo({
                url:
                  "/owner-app/pages/input-wx-pw/index?phone=" + this.data.phone,
              });
            }
            //调用微信绑定
            else if (this.data.registerStatus === 1) {
              wx.login({
                success: (res) => {
                  if (res.code) {
                    user
                      .wxBinding(res.code, this.data.phone, avatarUrl, nickName)
                      .then((res) => {
                        let status = res.result.registerStatus;
                        if (status === 4) {
                          wx.login({
                            success: (res) => {
                              if (res.code) {
                                user
                                  .wxLogin(res.code)
                                  .then((res) => {
                                    let registerStatus =
                                      res.result.registerStatus;
                                    if (registerStatus === 4) {
                                      const { user, token } = res.messageBody;
                                      wx.setStorageSync("user", user);
                                      wx.setStorageSync("token", token);
                                      wx.switchTab({
                                        url: "/pages/home/index",
                                      });
                                    }
                                  })
                                  .catch((err) => {
                                    console.log(err);
                                  });
                              }
                            },
                          });
                        }
                      })
                      .catch((err) => {
                        console.log(err);
                      });
                  }
                },
              });
            } else if (this.data.registerStatus === 5) {
              wx.navigateTo({
                url:
                  "/owner-app/pages/input-wx-pw/index?phone" + this.data.phone,
              });
            }
          }
        })
        .catch((err) => {
          console.log(err);
        });
    }
  },
  getCode() {
    user
      .registerGetAuthCode(this.data.phone, 3)
      .then((res) => {
        hint("验证码发送成功");
        let status = res.result.registerStatus;
        this.setTime();
        //手机号未注册，
        if (status === 0) {
          this.setData({
            code: res.result.checkCode,
            registerStatus: status,
          });
        }
        //手机号已注册，只进行微信绑定
        else if (status === 1) {
          this.setData({
            code: res.result.checkCode,
            registerStatus: status,
          });
        }
        //手机号已注册，没设置密码
        else if (status === 5) {
          this.setData({
            code: res.result.checkCode,
            registerStatus: status,
          });
        }
      })
      .catch((err) => {
        console.log(err);
      });
  },
  getPhoneCode() {
    if (this.data.phone === "") {
      wx.showToast({ title: "手机号不能为空", icon: "none", duration: 2000 });
      return;
    }
    if (!this.data.isDisabled) {
      this.getCode();
    } else {
      wx.showToast({ title: "请勿频繁点击", icon: "none", duration: 2000 });
    }
  },
  setTime() {
    let time = 60;
    let i = 0;
    let that = this;
    //1.在60秒之内，button上倒计时60秒，并显示
    //2.向controller请求验证码，并发送到指定的手机号上面
    let set = setInterval(() => {
      this.setData({
        codeText: --time + "秒",
      });
      i++;
      //此变量用于标识只调用一次获取验证码的函数
      if (i == 1) {
        //使获取验证码按钮不可点击
        this.setData({
          isDisabled: true,
        });
      }
    }, 1000);

    //60秒之后需要做的事情
    setTimeout(() => {
      //恢复获取验证码按钮可点击
      this.setData({
        isDisabled: false,
        codeText: "获取验证码",
      });
      clearInterval(set); //清除计时器
    }, time * 1000); //60000
  },
});
